package com.fortune.controller;


import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.DUserMgtExcution;
import com.fortune.dto.excution.MUserMgtExcution;
import com.fortune.dto.in.dUserMgt.DUserAddDto;
import com.fortune.dto.in.dUserMgt.DUserEditDto;
import com.fortune.dto.in.dUserMgt.DUserListDto;
import com.fortune.dto.in.mUserMgt.MUserListDto;
import com.fortune.entity.DUserInfo;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.DUserMgtEnum;
import com.fortune.enums.MUserMgtEnum;
import com.fortune.service.DUserMgtService;
import com.fortune.vo.dUserMgt.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Api(value = "duser", description="经销商员工管理", tags = {"经销商员工管理"})
@RestController
@RequestMapping("dUserMgt")
@Validated
public class DUserMgtController {
	@Autowired
	private DUserMgtService dUserMgtService;



	@ApiOperation(value = "经销商员工列表", nickname = "dUserList", notes = "@岳希伟")
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
	})
	@GetMapping(value = "list",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<User>> list(HttpServletRequest request, @Validated @ApiIgnore DUserListDto listDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		listDto.setPage((listDto.getPage()-1)*10);
		PagingData<User> pagingData = dUserMgtService.pagingData(companyId,dealerId, listDto);
		return new MUserMgtExcution<PagingData<User>>(pagingData, MUserMgtEnum.SUCCESS);
	}


	@ApiOperation(value = "添加经销商员工", nickname = "dUserAdd", notes = "@岳希伟")
	@PostMapping(value = "add",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> add(HttpServletRequest request,@Validated @RequestBody DUserAddDto addDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int result = dUserMgtService.addEmployer(companyId,dealerId, addDto);
		if (result==2) {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.PHONEFALSE);
		}else if (result==3) {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.NUMBERFALSE);
		}else if (result==1) {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.SUCCESS);
		} else {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.FAILSE);
		}
	}



	@ApiOperation(value = "编辑经销商员工", nickname = "dUserEdit", notes = "@岳希伟")
	@PostMapping(value = "edit",
			produces = { MediaType.APPLICATION_JSON_VALUE },
			consumes = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> edit(HttpServletRequest request,@Validated @RequestBody DUserEditDto dUserEditDto) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		String id=dUserInfo.getId();
		int result = dUserMgtService.editEmployer(companyId,dealerId,id, dUserEditDto);
		if (result==2) {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.PHONEFALSE);
		}else if (result==3) {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.NUMBERFALSE);
		}else if (result==1) {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.SUCCESS);
		} else {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.FAILSE);
		}
	}



	@ApiOperation(value = "删除经销商员工", nickname = "dUserDelete", notes = "@岳希伟")
	@ApiImplicitParam(name="id", value="用户id",paramType="query",dataType="string")
	@DeleteMapping(value = "delete",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> delete(HttpServletRequest request,@Validated @NotBlank(message="用户id不能为空") String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int result = dUserMgtService.editDel(companyId,dealerId,id);
		if(result>0){
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.SUCCESS);
		}else {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.FAILSE);
		}
	}

	@ApiOperation(value = "允许登录", nickname = "dUserAllow", notes = "@岳希伟")
	@ApiImplicitParam(name="id", value="用户id",paramType="query",dataType="string")
	@PostMapping(value = "allow",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> allow(HttpServletRequest request,@Validated @NotBlank(message="用户id不能为空") String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int result = dUserMgtService.editAllow(companyId,dealerId,id);
		if(result>0){
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.SUCCESS);
		}else {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.FAILSE);
		}
	}


	@ApiOperation(value = "禁止登录", nickname = "dUserBan", notes = "@岳希伟")
	@ApiImplicitParam(name="id", value="用户id",paramType="query",dataType="string")
	@PostMapping(value = "ban",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> ban(HttpServletRequest request,@Validated @NotBlank(message="用户id不能为空") String id) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int result = dUserMgtService.editBan(companyId,dealerId,id);
		if(result>0){
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.SUCCESS);
		}else {
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.FAILSE);
		}
	}


	@ApiOperation(value = "重置密码", nickname = "dUserPassword", notes = "@岳希伟")
	@ApiImplicitParams(
		{
			@ApiImplicitParam(name="id", value="用户id",paramType="query",dataType="string"),
			@ApiImplicitParam(name="oldPassword", value="旧密码",paramType="query",dataType="string"),
			@ApiImplicitParam(name="newPassword", value="新密码",paramType="query",dataType="string")
		}
	)

	@PostMapping(value = "resetPassword",
			produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> ban(HttpServletRequest request,@Validated @NotBlank(message="用户id不能为空") String id, @NotBlank String oldPassword,@NotBlank String newPassword) {
		HttpSession session = request.getSession();
		DUserInfo dUserInfo = (DUserInfo) session.getAttribute(Constants.SESSION_D_USER);
		String companyId=dUserInfo.getmId();
		String dealerId=dUserInfo.getdId();
		int result = dUserMgtService.editPassword(companyId,dealerId,id,oldPassword,newPassword);
		if(result>0){
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.SUCCESS);
		}else {
			if(result==-1){
				return new DUserMgtExcution<Void>(null, DUserMgtEnum.FAILSE);
			}
			return new DUserMgtExcution<Void>(null, DUserMgtEnum.FAILSE);
		}
	}

}
